import { redirect } from 'react-router-dom'

export default async ({ request, params }) => {
  const formData = await request.formData()
  const data = Object.fromEntries(formData)
  // 获取分类id,有就是在当前分类新建的；在所有片段中新建的,默认分类id为0，在未分类中
  const categoryId = params.categoryId || 0

  switch (request.method) {
    // 新增
    case 'POST': {
      const id = await window.api.sql(
        `insert into contents (title, content,category_id,created_at) values ('未命名片段', '',${categoryId},datetime('now', '+8 hours'))`,
        'insert'
      )
      console.log(id, 16)

      return redirect(`/config/category/contentList/${categoryId}/content/${id}`)
    }

    // 删除
    case 'DELETE': {
      return await window.api.sql(`delete from contents where id = ${data.id}`, 'del')
    }
  }

  return {}
}
